<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Carbon\Carbon;

class WechatUsersSeeder extends Seeder
{
    public function run()
    {
        $usedNicknames = [];
        $batchSize = 1000; // 每次插入1000条数据
        $totalRecords = 90000;
        
        for ($i = 0; $i < $totalRecords; $i += $batchSize) {
            $data = [];
            
            for ($j = 0; $j < $batchSize && ($i + $j) < $totalRecords; $j++) {
                do {
                    $nickname = Str::random(rand(1, 8));
                } while (in_array($nickname, $usedNicknames));
                
                $usedNicknames[] = $nickname;
                
                $data[] = [
                    'openid' => Str::random(28),
                    'session_key' => base64_encode(Str::random(16)),
                    'nickname' => $nickname,
                    'avatar' => null,
                    'gender' => rand(0, 2),
                    'country' => null,
                    'province' => null,
                    'city' => null,
                    'created_at' => Carbon::now(),
                    'updated_at' => Carbon::now(),
                ];
            }
            
            DB::table('wechat_users')->insert($data);
            echo "已插入 " . min($i + $batchSize, $totalRecords) . " 条数据\n";
        }
    }
} 